// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

//static const TCHAR logRecFile[] = _T("track.rec");

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
		g_hIns = hModule;
		g_MOUSE_UWM_HOOK = RegisterWindowMessage(UWM_MOUSE_HOOK_MSG);
		g_KEYBOARD_UWM_HOOK = RegisterWindowMessage(UWM_KEYBOARD_HOOK_MSG);

		if (!g_MOUSE_UWM_HOOK)
			return FALSE;

		if(!lstrcmp(s_sHookConf.logFile, _T("")))
		{
			GetModuleFileName(hModule, s_sHookConf.logFile, MAX_PATH);
			int i = lstrlen(s_sHookConf.logFile) - 1;
			while (s_sHookConf.logFile[i] != _T('\\'))
				s_sHookConf.logFile[i--] = _T('\0');
			lstrcat(s_sHookConf.logFile, _T("track.rec"));						// xxx/track.rec
		}
		// 1. existed?
		if (_taccess_s(s_sHookConf.logFile, 0) != 0)
		{
			FILE *fp = NULL;
			if ((fp = _tfopen(s_sHookConf.logFile, _T("ab"))) == NULL)
			{
				return FALSE;
			}
			LogHeader header = {0xAAAAAAAA, 0x01, sizeof(TCHAR), _T("Initial version")};	// log file is ANSI or UTF-8
			fwrite(&header, sizeof(LogHeader), 1, fp);
			fwrite(s_tFiles, MAX_PATH * sizeof(TCHAR), APP_COUNT, fp);
			fclose(fp);
		}
		break;

	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}

